package com.zsrt.oa.dept.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsrt.oa.dept.entity.DeptEntity;
import com.zsrt.oa.dept.service.DeptService;
import com.zsrt.oa.dept.service.impl.DeptServiceImpl;
import com.zsrt.utils.DBUtils;

/**
 * 部门控制层
 * @author 罗智壕-13424431416
 *
 * 2017年6月3日
 */
public class DeptController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 实例化部门的服务层
	 */
    private DeptService service = new DeptServiceImpl();
    
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) 
    		throws ServletException, IOException {
    	
    	String type = request.getParameter("type");
    	
    	//防止type是空指针
    	if(null != type && !"".equals(type.trim())){
    		Map<String,Object> map = new HashMap<String,Object>();
    		DeptEntity deptEntity = new DeptEntity();
    		if("add".equals(type)){
    			String deptName = request.getParameter("deptName");
    			deptEntity.setDeptName(deptName);
    			
    			//调用部门增加的方法
    			service.add(deptEntity,map);
    		}else if("query".equals(type)){
    			String pageSize = request.getParameter("pageSize");
    			String curPage  = request.getParameter("curPage");
    			
    			//调用部门分页查询的方法
    			service.query(pageSize, curPage, map);
    		}else if("delete".equals(type)){
    			String deptId = request.getParameter("deptId");
    			deptEntity.setDeptId(deptId);
    			
    			//调用部门指定删除的方法
    			service.delete(deptEntity, map);
    		}else if("updateQuery".equals(type)){
    			String deptId = request.getParameter("deptId");
    			deptEntity.setDeptId(deptId);
    			
    			//调用部门修改填充的方法
    			service.updateQuery(deptEntity, map);
    		}else if("update".equals(type)){
    			String deptId = request.getParameter("deptId");
    			String deptName = request.getParameter("deptName");
    			deptEntity.setDeptId(deptId);
    			deptEntity.setDeptName(deptName);
    			
    			//调用部门修改的方法
    			service.update(deptEntity, map);
    		}
    		
    		
    		DBUtils.toJson(map, response);
    	}
    }
}
